﻿#include "Paint.h"
#include <QDebug>

Paint::Paint(const VPoint &top_left, const VPoint &bottom_right)
{
    this->site = bottom_right - top_left;
    this->_map = new QPixmap(this->site.x(), this->site.y());
    this->site = top_left;
}

Paint::Paint(const QString &medium, const QVector<VPoint> &points, double radius)
{

    VPoint top_left;
    VPoint bottom_right;

    if(points.length() > 0)
    {
        top_left   =   points[0] - VPoint(radius, radius);
        bottom_right = points[0] + VPoint(radius, radius);
    }

    foreach(VPoint point, points)
    {
        if(top_left.x()   >   point.x() - radius){top_left.setX(point.x() - radius);}
        if(top_left.y()   >   point.y() - radius){top_left.setY(point.y() - radius);}
        if(bottom_right.x() < point.x() + radius){bottom_right.setX(point.x() + radius);}
        if(bottom_right.y() < point.y() + radius){bottom_right.setY(point.y() + radius);}
    }

    VPoint size = bottom_right - top_left;
    this->_map = new QPixmap(size.x(), size.y());
    this->site = top_left;

    //qDebug() << medium << size.x() << size.y();
    //qDebug() << medium << this->_map->height() << this->_map->width();

    this->paintMap(medium, points, radius);
}

Paint::~Paint()
{
    delete this->_map;
}

void Paint::draw(const VPoint &site, const Paint &paint)
{
    QPainter painter(this->_map);

    painter.drawPixmap(site.toQPointF() + paint.site.toQPointF(), paint.pixmap());
}

void Paint::clear()
{
    this->_map->fill(QColor(0,0,0,0));
}

const QPixmap & Paint::pixmap() const
{
    return *this->_map;
}

const VPoint & Paint::delta() const
{
    return this->site;
}

void Paint::paintMap(const QString &medium,const QVector<VPoint> &points, double radius)
{
    this->_map->fill(QColor(0,0,0,32));//测试用,检查画布范围
    QPainter painter(this->_map);
//    QPixmap pixmap(":/" + medium);
//    QBrush bru(pixmap);
//    painter.setPen(QColor(0,0,0,0));
//    painter.setBrush(bru);

    QVector<QPointF> paint_pos;

    //将VPoint根据实际位置转化为QPointF
    //以此画布为基准，将原点坐标系的坐标转化为Qt默认坐标系的坐标（不是画面为基准的坐标）
    foreach(VPoint po, points)
    {
        paint_pos.append((po - this->site).toQPointF());
    }

    //画多边形外框
    painter.drawPolygon(paint_pos.data(), paint_pos.length());

    qDebug() << medium << "here 1";

    //if(radius > 0)
    {
        //画点上的球体
        foreach(QPointF po, paint_pos)
        {
            painter.drawEllipse(po, radius, radius);
        }

        QPointF paint_poy[4];

        //画外框到球体的补充条
        for(int i = paint_pos.length() - 1; i >= 0; --i)
        {
            //以其中两圆点为基准
            if(i == 0)  paint_poy[0] = paint_pos.last();
            else        paint_poy[0] = paint_pos[i - 1];

            paint_poy[1] = paint_pos[i];

            VPoint po;
            po.fromQPointF(paint_poy[1] - paint_poy[0]);//基准圆点之间的线段，且有方向，得到基准线段
            po.setAngle(po.angle() + VPoint(0, 1).angle());//VPoint(0, 1).angle()等于 +PI/2，使基准线段顺时针旋转90度
            po.setLength(radius);//设置圆点的半径长度，使外框与圆点边缘重合

            paint_poy[2] = paint_poy[1] + po.toQPointF();//外框在圆点上的点
            paint_poy[3] = paint_poy[0] + po.toQPointF();//外框在圆点上的点

            painter.drawPolygon(paint_poy, 4);//画出两个基准圆点到两个基准圆点边缘的矩形
        }

        qDebug() << medium << "here 2";
    }
    qDebug() << medium << "here 3";
}
